<template>
	<view class="page-wrap" :data-theme="theme" :class="theme" :style="globalColor.style">
		<HeadCustom :isAppBtn="false"></HeadCustom>
		<view class="wrap">
			<view class="title font46 font-weight">附件简历</view>
			<view class="tips mt16 font30 font-w300">仅支持上传一份附件简历，如需替换请先删除后再上传新的附件简历。</view>
			<view class="expect-wrap mt32">
				<view v-if="fromData.fileUrl" class="file-item  fboxRow Ycenter bg-21242C">
					<image class="icon-file" src="https://ebc-prod.cdn.benwunet.com/uni-static/workplace/icon-file.png" mode="aspectFill"></image>
					<view class="fboxCol flex1">
						<view class="tit font30 lh44">{{fromData.fileName}}</view>
						<view class="con font26 lh38 color-6B707C font26">{{fromData.gmtUpLoad}} {{fromData.fileSize}}</view>
					</view>
					<view class="action" @click="moreAction">
						<u-icon name="more-dot-fill" size="36rpx"></u-icon>
					</view>
					<ActionSelect pattern="top:94rpx;right: 0" arrowPattern="right: 40rpx" :isShow.sync="isAction" :list="actionList" @action="actionClick" />
				</view>
				<view v-else class="upload-btn fboxCol Ycenter Xcenter" @tap="btnUploadFile">
					<view class="fboxRow Ycenter Xcenter lh44 font30 color-ACB4C7"><view class="action-jiahao fboxRow Ycenter Xcenter"><text class="bwfont bw-jiahao font18"></text></view><text style="margin-left: 8rpx;">上传附件简历</text></view>
					<view class="fboxRow Ycenter Xcenter mt10 lh38 font26 color-6B707C">建议优选BOSS简历文件上传</view>
				</view>
			</view>
		</view>
		<ModelPublic ref="modelRef" content="您确定要删除吗？" @confirm="delFile" />
	</view>
</template>

<script>
	import {fileUploadAnon} from '@/api/index.js';
	import {recruitMixins} from '../common/recruit.mixins.js';
	import ActionSelect from '../components/action-select.vue';
	import ModelPublic from '@/components/model-public/index.vue';
	import {timestampToTime} from '@/static/js/common.js';
	import { resumeInfo } from '@/api/recruit.js';
	
	export default {
		mixins: [recruitMixins],
		components: {ActionSelect, ModelPublic},
		data() {
			return {
				theme: getApp().globalData.appTheme,
				globalColor: getApp().globalData.globalColor,
				fromData: {
					"fileSize": "",
					"fileUrl": "",
					"gmtUpLoad": "",
					"name": "",
					fileName: ''
				},
				vitaTemp: {},
				isAction: false,
				actionList:[{
					value: 1,
                    label: '查看',
                }, {
					value: 2,
                    label: '下载',
                }, {
					value: 3,
                    label: '删除',
                }]
			}
		},
		onLoad() {
			this.getInit()
		},
		mounted() {
			// #ifdef H5
			window.appFileCallback = this.appFileCallback
			// #endif
		},
		methods: {
			moreAction(){
				this.isAction = !this.isAction
			},
			actionClick(e){
				if(e.value.value == 1){
					const data = { type: 'vitaLook', fileUrl: this.fromData.fileUrl,fileName:this.fromData.name }
					this.$bridge.callHandler('recruitHandler', data, () => {
						console.log('recruitHandler', '返回')
					})
				}else if(e.value.value == 2){
					const data = { type: 'vitaDownload', fileUrl: this.fromData.fileUrl,fileName:this.fromData.name }
					this.$bridge.callHandler('recruitHandler', data, () => {
						console.log('recruitHandler', '返回')
					})
				}else{
					this.$refs.modelRef.show = true
				}
			},
			getInit() {
				const vitaTemp = getApp().globalData.vitaTemp
				if(vitaTemp.hasOwnProperty('accessoryData')){
					this.vitaTemp = vitaTemp
					this.fromData = vitaTemp.accessoryData || { "fileSize": "", "fileUrl": "", "gmtUpLoad": "", "name": ""}
					this.setFileName()
				}else{
					resumeInfo().then(res => {
						if (res.data) {
							getApp().globalData.vitaTemp = res.data;
							this.vitaTemp = res.data
							this.fromData = res.data.accessoryData || { "fileSize": "", "fileUrl": "", "gmtUpLoad": "", "name": ""}
							this.setFileName()
						}
					});
				}
			},
			setFileName(){
				const fileName = this.fromData.name
				if(fileName){
					const arr = fileName.split('.')
					if(arr[0].length > 10){
						this.fromData.fileName = arr[0].substring(0,3)+'*****'+arr[0].substring(arr[0].length-4,arr[0].length-1)+'.' + arr[arr.length-1]
					}else{
						this.fromData.fileName = fileName
					}
				}
			},
			delFile(){
				this.fromData.fileUrl = ''
				this.fromData.fileSize = ''
				this.fromData.gmtUpLoad = ''
				this.fromData.name = ''
				this.$refs.modelRef.show = false
				this.save()
			},
			btnUploadFile(){
				// #ifdef H5
				const {bw_link} = getApp().getBWData(['bw_link'])
				if(bw_link.isApp){
					let u = navigator.userAgent;
					if (!/(iPhone|iPad|iPod|iOS)/i.test(u)) {
						try{
							const data = { type: 'fileUpload', extension: ['.doc', '.docx', '.pdf']}
							window.android.recruitHandler(JSON.stringify(data))
						}catch(err){
							console.log(err)
						}
					}else{
						this.uploadFile()
					}
				}else{
					this.uploadFile()
				}
				// #endif
				// #ifdef MP
					this.uploadFile()
				// #endif
			},
			uploadFile(){
				uni.chooseFile({
					count:1,
					extension: ['.doc', '.docx', '.pdf'],
					success: (resFile) => {
						console.log(resFile)
						const file = resFile.tempFiles[0]
						fileUploadAnon(file).then(res=>{
							if(res.data){
								this.fromData.fileUrl = res.data.url
								this.fromData.fileSize = this.getfilesize(file.size)
								this.fromData.gmtUpLoad = res.data.date
								this.fromData.name = resFile.tempFiles[0].name
								this.setFileName()
								this.save()
							}
						})
					},fail(err) {
					}
				})
			},
			appFileCallback(resFile){
				this.fromData.fileUrl = resFile.fileUrl
				this.fromData.fileSize = this.getfilesize(resFile.fileSize)
				this.fromData.gmtUpLoad = timestampToTime(new Date().getTime())
				this.fromData.name = resFile.fileName
				this.setFileName()
				this.save()
			},
			getfilesize(size) {
				if (!size)
				return "";
				var num = 1024; //byte
				if (size < num)
					return size + "B";
				if (size < Math.pow(num, 2))
					return (size / num).toFixed(2) + "KB"; //kb
				if (size < Math.pow(num, 3))
					return (size / Math.pow(num, 2)).toFixed(2) + "M"; //M
				if (size < Math.pow(num, 4))
					return (size / Math.pow(num, 3)).toFixed(2) + "G"; //G
				return (size / Math.pow(num, 4)).toFixed(2) + "T"; //T
			},
			save() {
				if(this.fromData.fileUrl){
					this.vitaTemp.accessoryData = this.fromData
				}else{
					this.vitaTemp.accessoryData = {}
				}
				this.postSave(this.vitaTemp)
			},
		}
	}
</script>

<style lang="scss" scoped>
	.page-wrap {
		min-height: calc(100vh);
	}

.title { line-height: 66rpx; color:var(--contourButtonTextColor) }
.tips { line-height: 44rpx; color: var(--contentFontLightColor) }
.tit{color:var(--contentFontDarkColor);}


	.wrap {
		padding: 40rpx;
	}
	// .upload-btn{width: 670rpx;height: 250rpx;border-radius: 12rpx;border: 1rpx dashed #E8CDA7;}
	.upload-btn{width: 100%;height: 250rpx;border-radius: 12rpx;border: 2rpx dashed #E8CDA7;}
	.file-item{position: relative; padding: 32rpx; width: 670rpx;height: 144rpx;border-radius: 12rpx;}
	.file-item .icon-file{width: 80rpx;height: 80rpx;margin-right: 10rpx;}
.action-jiahao{width: 32rpx;height: 32rpx; line-height: 32rpx;overflow: hidden;border: 1px solid var(--contentFontFourColor);color: var(--contentFontFourColor);border-radius: 50%;}

</style>